<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class tire_tread_wear_create extends Page
{
       public $hfCurrOdTime = null;
       public $editKmRun = null;
       public $editTripDistance = null;
       public $cmbTrack = null;
       public $Label5 = null;
       public $Label4 = null;
       public $Label3 = null;
       public $lblod = null;
       public $lblpressure = null;
       public $lbltread = null;
       public $Pressure = null;
       public $Label2 = null;
       public $Label1 = null;
       public $editPressure = null;
       public $editOdometer = null;
       public $editTread = null;
       public $Label12 = null;
       public $JSValidate = null;
       public $hfError = null;
       public $JTSiteTheme1 = null;
       public $Image1 = null;
       public $hfMountedPost = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $hfMountedOn = null;
       public $lblMount = null;
       public $hfTireId = null;
       function editTripDistanceJSBlur($sender, $params)
       {

       ?>
       var kmrun = Number(document.getElementById('editOdometer').value) * Number(document.getElementById('editTripDistance').value);
       document.getElementById('editKmRun').value = kmrun;
       <?php

       }

       function tire_tread_wear_createJSLoad($sender, $params)
       {

       ?>
       cmbTrackJSChange();

       <?php

       }

       function cmbTrackJSChange($sender, $params)
       {

       ?>
       var track = document.getElementById('cmbTrack').value;
       if(track=="Odometer" || track=="Hours")
       {
        document.getElementById("Label2").innerHTML = track;
        document.getElementById("editKmRun_outer").style.marginTop = "124";
        document.getElementById("Label4").style.marginTop = "128";

        document.getElementById("editTripDistance_outer").style.marginTop = "164";
        document.getElementById("Label3").style.marginTop = "168";
        document.getElementById("editTripDistance_outer").style.display = "none";
        document.getElementById("Label3_outer").style.display = "none";
       }
       if(track=="Trip")
       {
        document.getElementById("Label2").innerHTML = "Number of trips";
        document.getElementById("editKmRun_outer").style.marginTop = "164";
        document.getElementById("Label4").style.marginTop = "168";

        document.getElementById("editTripDistance_outer").style.marginTop = "124";
        document.getElementById("Label3").style.marginTop = "128";
        document.getElementById("editTripDistance_outer").style.display = "inline";
        document.getElementById("Label3_outer").style.display = "inline";
       }


       <?php

       }

       function editOdometerJSBlur($sender, $params)
       {

       ?>
       validateRequire('editOdometer','lblod');

       var track = document.getElementById('cmbTrack').value;
       var thisfield = document.getElementById('editOdometer').value;
       if(track=="Odometer" || track=="Hours")
       {
        var kmrun = Number(thisfield) - Number(document.getElementById('hfCurrOdTime').value);
       }
       if(track=="Trip")
       {
        var kmrun = Number(thisfield) * Number(document.getElementById('editTripDistance').value);
       }
       document.getElementById('editKmRun').value = kmrun;
       <?php

       }

       function editPressureJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('editPressure','lblpressure');
       <?php

       }

       function editTreadJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('editTread','lbltread');
       <?php

       }


       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->editTread->Text, 'lbltread');
       $this->validateRequired($this->editPressure->Text, 'lblpressure');
       //'$this->validateRequired($this->editOdometer->Text, lblod);
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       $track = $this->cmbTrack->getItemIndex();

       if($track=='Odometer' || $track=='Hours')
       {
       $query_ins = "INSERT INTO tire_tread_wear_tbl (tire_id, tread, pressure, trackby, kmrun, odometer, hours, mounted_on, mounted_post, insert_date)
       VALUES
       ('".$this->hfTireId->getValue()."','".$this->editTread->Text."','".$this->editPressure->Text."','".$track."','".$this->editKmRun->Text."','".$this->editOdometer->Text."','".$this->editOdometer->Text."','".$this->hfMountedOn->getValue()."','".$this->hfMountedPost->getValue()."','".date('m-d-Y')."')";
       mysql_query($query_ins) or die("INSERT ".mysql_error());
       }
       if($track=='Trip')
       {
       $query_ins = "INSERT INTO tire_tread_wear_tbl (tire_id, tread, pressure, trackby, kmrun, number_of_trips, trip_distance, mounted_on, mounted_post, insert_date)
       VALUES
       ('".$this->hfTireId->getValue()."','".$this->editTread->Text."','".$this->editPressure->Text."','".$track."','".$this->editKmRun->Text."','".$this->editOdometer->Text."','".$this->editTripDistance->Text."','".$this->hfMountedOn->getValue()."','".$this->hfMountedPost->getValue()."','".date('m-d-Y')."')";
       mysql_query($query_ins) or die("INSERT ".mysql_error());
       }

       $query_up_tire = "UPDATE tire_inventory_tbl SET
       curr_tread='".$this->editTread->Text."',
       curr_psi='".$this->editPressure->Text."'
       WHERE tire_id='".$this->hfTireId->getValue()."'
       ";
       mysql_query($query_up_tire) or die("UPDATE TIRE ".mysql_error());

       echo '<script language=javascript>
       window.opener.location.reload();
       window.close();
       </script>';


       //redirect('tire_tread_wear_list.php?tire_id'.$this->hfTireId->getValue());
       }
       }

       function btnCancelJSClick($sender, $params)
       {
       ?>
       var tire_id = document.getElementById('hfTireId').value;
       //document.location.href = 'tire_tread_wear_list.php?tire_id='+tire_id;
       window.close();
       <?php
       }

        function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }

       function tire_tread_wear_createCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblpressure->Caption = '';
       $this->lbltread->Caption = '';
       $this->lblod->Caption = '';

       $get_tire_id = $_GET['tire_id'];
       if($get_tire_id!='' && $get_tire_id!=null && isset($get_tire_id))
       $this->hfTireId->setValue($get_tire_id);

       //$this->editOdometer->Text = '';
       $this->editPressure->Text = '';
       $this->editTread->Text = '';
       $this->hfMountedOn->setValue('0');

       $query_sel_tire = "SELECT status, mounted_on, mounted_post FROM tire_inventory_tbl WHERE tire_id='".$this->hfTireId->getValue()."'";
       $result_sel_tire = mysql_query($query_sel_tire) or die("SELECT TIRE ".mysql_error());
       $row_sel_tire = mysql_fetch_array($result_sel_tire);

       $status = $row_sel_tire['status'];
       if($status=='0')
       {
        $this->editOdometer->setReadOnly(true);
        $this->editOdometer->Text='';
        $this->lblMount->Caption = 'Tire not mounted';
       }
       if($status=='1')
       {
        $veh_id = $row_sel_tire['mounted_on'];
        $query_veh = "SELECT * FROM vehicle_tbl WHERE vehicle_id='".$veh_id."'";
        $reslt_veh = mysql_query($query_veh) or die("SELECT VEHICLE ".mysql_error());
        $row_veh = mysql_fetch_array($reslt_veh);

        $track_by = $row_veh['track_by'];

        $this->cmbTrack->Clear();
        if($track_by=='distance')
        {
        $od = $row_veh['curr_tracking_value'];
        $this->cmbTrack->AddItem("Odometer","","Odometer");
        }

        if($track_by=='hours')
        {
        $od = $row_veh['curr_hours'];
        $this->cmbTrack->AddItem("Hours","","Hours");
        }
        $this->cmbTrack->AddItem("Trip","","Trip");
        $this->hfCurrOdTime->setValue($od);

        $plate_num = $row_veh['veh_num'];
        $post = $row_sel_tire['mounted_post'];
        $veh_id = $row_veh['vehicle_id'];

        $this->hfMountedOn->setValue($veh_id);
        $this->hfMountedPost->setValue($post);
        $this->editOdometer->setReadOnly(false);
        $this->editOdometer->Text = $od;
        $this->lblMount->Caption = 'Mounted on '.$post.' of vehicle '.$plate_num;
       }
       }

}

global $application;

global $tire_tread_wear_create;

//Creates the form
$tire_tread_wear_create=new tire_tread_wear_create($application);

//Read from resource file
$tire_tread_wear_create->loadResource(__FILE__);

//Shows the form
$tire_tread_wear_create->show();

?>